package com.util;

import org.apache.http.Header;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import cn.yueliangbaba.R;

public class MgqDataHandler extends AsyncHttpResponseHandler {
	public final static String TAG = "MgqDataHandler";
	private Context context;
	private boolean showDialog;
	private boolean showError;
	private boolean clearCookie;
	private Dialog progressDialog;

	public boolean isShowDialog() {
		return showDialog;
	}

	public void setShowDialog(boolean showDialog) {
		this.showDialog = showDialog;
	}

	public boolean isShowError() {
		return showError;
	}

	public void setShowError(boolean showError) {
		this.showError = showError;
	}

	public MgqDataHandler(Context ctx, boolean showDialog, boolean clearCookie) {

		this.showDialog = true;
		this.showError = true;
		context = ctx;
		this.showDialog = showDialog;

	}

	public void onFailed(Throwable ee) {
	}

	public void onStart() {
		if (checkNetwork()) {

			if (showDialog) {
				progressDialog = new Dialog(context, R.style.dialog);
				progressDialog.setContentView(R.layout.dialog_wait);
				progressDialog.setCancelable(true);
				progressDialog.show();
			}
		} else {
			Toast.makeText(context, "网络连接不可用", 1500).show();
		}
		super.onStart();
	}

	public void onFailure(Throwable ble) {
		ToastUtil.showToastWithAlertPic("网络连接失败");
	}

	public void onSuccess(String response) {
		try {

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (showDialog) {
				if (progressDialog != null) {
					progressDialog.dismiss();
				}
			}
		}
	}

	// public void onSuccess(String response);

	private boolean checkNetwork() {

		String service = Context.CONNECTIVITY_SERVICE;
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(service);
		NetworkInfo activeNetwork = connectivity.getActiveNetworkInfo();
		if (null == activeNetwork) {
			return false;
		}
		return activeNetwork.isConnected();
	}

	@Override
	public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
		try {
			String response = new String(responseBody, "UTF-8");
			onSuccess(response);
			// TODO-如果response全是json对象则可以对需要做认证的网络进行判断
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onFailure(int statusCode, Header[] headers,
			byte[] responseBody, Throwable error) {
		if (showDialog) {
			if (NetworkProber.isNetworkAvailable(context)) {
				Toast.makeText(context, "数据请求异常,请稍后再试", Toast.LENGTH_SHORT)
						.show();
			} else {
				Toast.makeText(context, "网络连接不可用", Toast.LENGTH_SHORT).show();
			}
		}
	}

	@Override
	public void onCancel() {
		super.onCancel();
		dismissLoading();
	}

	@Override
	public void onProgress(int bytesWritten, int totalSize) {
		super.onProgress(bytesWritten, totalSize);
	}

	@Override
	public void onRetry(int retryNo) {
		super.onRetry(retryNo);
	}

	@Override
	public void onFinish() {
		super.onFinish();
		dismissLoading();
	}

	private void dismissLoading() {
		if (showDialog) {
			if (progressDialog != null) {
				try{
					if(progressDialog.isShowing()) progressDialog.dismiss();
				}
				catch(Exception e){}
			}
		}
	}

}
